package com.xjtu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xjtu.dto.medicationRecord.MedicationDto;
import com.xjtu.entity.Medication;
import com.xjtu.mapper.MedicationMapper;
import com.xjtu.result.PageResult;
import com.xjtu.result.Result;
import com.xjtu.service.MedicationService;
import com.xjtu.vo.billRecord.BillVo;
import com.xjtu.vo.medication.MedicationDetailVo;
import com.xjtu.vo.medication.MedicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MedicationServiceImpl implements MedicationService {

    @Autowired(required = false)
    MedicationMapper medicationMapper;

    @Override
    public void insert(MedicationDto medicationDto) {
        log.info("新增参数为{}", medicationDto);
        Medication medication = new Medication();
        BeanUtils.copyProperties(medicationDto, medication);
        log.info("新增记录为{}", medication);
        medicationMapper.insert(medication);
        if(medication.getId() == 0) {
            throw new RuntimeException("插入失败");
        }
    }

    @Override
    public PageResult getTodayFinish(Integer page, Integer perPage) {
        PageHelper.startPage(page, perPage);
        List<MedicationVo> medicationVos = medicationMapper.getTodayFinish();
        PageInfo<MedicationVo> pageInfo = new PageInfo<>(medicationVos);
        System.out.println("pageInfo = " + pageInfo);

        Long total = pageInfo.getTotal();
        List<MedicationVo> records = pageInfo.getList();
        return new PageResult(total, records);
    }

    @Override
    public PageResult getToday(String elderlyName, Integer page, Integer perPage) {
        PageHelper.startPage(page, perPage);
        List<MedicationVo> medicationVos = elderlyName.equals("null") ? medicationMapper.getTodayList() : medicationMapper.getToday(elderlyName);
        PageInfo<MedicationVo> pageInfo = new PageInfo<>(medicationVos);
        System.out.println("pageInfo = " + pageInfo);

        Long total = pageInfo.getTotal();
        List<MedicationVo> records = pageInfo.getList();
        return new PageResult(total, records);


    }

    @Override
    public PageResult getMedication(String elderlyName, Integer page, Integer perPage) {
        PageHelper.startPage(page, perPage);
        List<MedicationDetailVo> medicationDetailVos = elderlyName.equals("null") ? medicationMapper.getList() : medicationMapper.getMedication(elderlyName);
        PageInfo<MedicationDetailVo> pageInfo = new PageInfo<>(medicationDetailVos);
        System.out.println("pageInfo = " + pageInfo);

        Long total = pageInfo.getTotal();
        List<MedicationDetailVo> records = pageInfo.getList();
        return new PageResult(total, records);

    }

    @Override
    public Result changeById(MedicationDto medicationDto) {
        Medication medication = new Medication();
        BeanUtils.copyProperties(medicationDto, medication);
        int number = medicationMapper.updateByPrimaryKey(medication);
        if(number <= 0) {
            return Result.error("修改失败");
        }
        return Result.success();
    }

    @Override
    public Result deleteById(Long id) {
        int number = medicationMapper.deleteByPrimaryKey(id);
        if(number <= 0) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
